package com.changanw.provider.controller;

import com.changanw.api.IUserService;
import com.changanw.commons.User;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

@RestController
public class HelloController implements IUserService {
    @Value("${server.port}")
    Integer port;


    @Override
    @RateLimiter(name = "rlA")
    public String hello(){
        String s = "hello eurkea " + port;
        System.out.println(new Date());
        //int i = 1/0;
        return s;
    }

    @Override
    public String hello2(String name){
        System.out.println(new Date() + ">>>" + name);
        return "hello eurkea " + name;
    }

    /**
     * key   value  方式
     * @param user
     * @return
     */
    @PostMapping("/user1")
    public User addUser1(User user){
        return user;
    }

    /**
     * json方式
     * @param user
     * @return
     */

    @Override
    public User addUser2(@RequestBody User user){
        return user;
    }

    @PutMapping("/user1")
    public void updateUser1(User user){
        System.out.println(user);
    }
    @PutMapping("/user2")
    public void updateUser2(@RequestBody User user){
        System.out.println(user);
    }

    @DeleteMapping("/user1")
    public void deleteUser1(Integer id){
        System.out.println("已经删除"+id);
    }

    @Override
    public void deleteUser2(@PathVariable Integer id){
        System.out.println("已经删除"+id);
    }
    @DeleteMapping("/user3")
    public void deleteUser3(String name){
        System.out.println("已经删除"+ name);
    }

    @Override
    public void getuserByName(@RequestHeader String name) throws UnsupportedEncodingException {
        System.out.println(URLDecoder.decode(name,"UTF-8"));
    }
}
